package drukarnia.controller;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import drukarnia.model.PriceForMaterials;
import drukarnia.util.HibernateUtil;

public class PriceForMaterialsManager extends HibernateUtil{
	
	public PriceForMaterials save(PriceForMaterials p4m){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(p4m);
		session.getTransaction().commit();
		return p4m;
	}
	
	public PriceForMaterials update(PriceForMaterials p4m, int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		PriceForMaterials pFm = (PriceForMaterials) session.load(PriceForMaterials.class, id);
		pFm.setTypeOfMaterial(p4m.getTypeOfMaterial());
		pFm.setPrice(p4m.getPrice());
		session.update(pFm);
		session.getTransaction().commit();
		return pFm;
	}
	
	public List<PriceForMaterials> fetchPriceFromDB(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<PriceForMaterials> p4mList = null;
		Query q = session.createQuery("from PriceForMaterials");
		p4mList = q.list();
		session.getTransaction().commit();
		return p4mList;
	}
	
	public List<PriceForMaterials> fetchPriceAtIdFromDB(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<PriceForMaterials> p4mList = null;
		Query q = session.createQuery("from PriceForMaterials where id=" + id);
		p4mList = q.list();
		session.getTransaction().commit();
		return p4mList;
	}
	
	public PriceForMaterials delete(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		PriceForMaterials pFm = (PriceForMaterials) session.load(PriceForMaterials.class, id);
		session.delete(pFm);
		session.getTransaction().commit();
		return pFm;
	}

}
